<?php

namespace app\api\controller;

use app\BaseController;
use think\facade\View;
use think\facade\Config;
// use think\facade\Session;
use app\common\model\SystemAdmin;
use app\common\model\User;
use app\common\model\Client;
use app\common\model\FileLog;
use app\common\controller\Handle;
use app\common\model\Store;
use think\facade\Request;

class ApiClient extends ApiBase
{
    public function initialize()
    {
        parent::initialize();
        $this->user = new User;
        $this->Client = new Client;
        $this->FileLog = new FileLog;
        $this->Store = new Store;
    }

    //添加客户
    public function addClient()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();

        
        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传客户名称"]);
        }

        if(empty($data['type'])){
            return json(['status' => 0, 'msg' =>"选择客户类型"]);
        }

        $add = [
            "name"=>$data['name'],
            "created_at"=>date("Y-m-d H:i:s"),
            "update_at"=>date("Y-m-d H:i:s"),
            "store_id"=> $store_id,
            "type"=> $data['type'],
        ];

        if($data['type'] == 1){
            if(empty($data['riqi'])){
                return json(['status' => 0, 'msg' =>"请上传日期"]);
            }

            if(empty($data['content'])){
                return json(['status' => 0, 'msg' =>"请上传订单详情"]);
            }

            if(empty($data['price'])){
                return json(['status' => 0, 'msg' =>"请上传金额"]);
            }

            $add['riqi'] = $data['riqi'];
            $add['content'] = $data['content'];
            $add['price'] = $data['price'];

            if(!empty($data['tel'])){
                $add['tel'] = $data['tel'];
            }

            
        }else{
            if(empty($data['title'])){
                return json(['status' => 0, 'msg' =>"请上传提醒标题"]);
            }

            if(empty($data['commemorative_date'])){
                return json(['status' => 0, 'msg' =>"请上传纪念日期"]);
            }

            if(empty($data['reminder_date'])){
                return json(['status' => 0, 'msg' =>"请上传提醒日期"]);
            }

            $add['title'] = $data['title'];
            $add['commemorative_date'] = $data['commemorative_date'];
            $add['reminder_date'] = $data['reminder_date'];

            if(!empty($data['tel'])){
                $add['tel'] = $data['tel'];
            }

            if(!empty($data['address'])){
                $add['address'] = $data['address'];
            }

            if(!empty($data['remark'])){
                $add['remark'] = $data['remark'];
            }

        }

        $res = $this->Client->create($add);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }

    // 客户列表
    public function listClient()
    {

        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');


        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];
        
        $data = request()->post();

        $where = [];

        $where[] = ["store_id","=",$store_id];

        if(!empty($data['keyword'])){
            $where[] = ["name","like", "%" . $data['keyword'] . "%"];
        }

        if(empty($data['limit'])){
            $data['limit'] = 10;
        }

        if(empty($data['page'])){
            $data['page'] = 1;
        }

        $list = $this->Client->getPagelist($where,"id,name,tel,type,price,title,riqi,commemorative_date,reminder_date,address",'id desc');

      

        $count = $this->Client->countRows($where);

        $pages = ceil($count/$data['limit']);

        return json(['status'=> 1,'data'=>$list,'pages'=>$pages,"count"=>$count]);
    }

      //客户详情
      public function getClient()
      {
          // 存储空间对应的域名
          $domain = config('sysconfig.qiniu_domain');
  
          // 判断店铺是否正常
          $is_store = $this->storeIsNormal($this->uid);
  
          if($is_store['code'] == 1){
              return json(['status' => 0, 'msg' =>$is_store['msg']]);
          }
   
          $store_id = $is_store['store_id'];
  
          $data = request()->post();
  
          if(empty($data['id'])){
              return json(['status' => 0, 'msg' =>"请上传客户ID"]);
          }
  
          $list = $this->Client->getOne(['id' =>$data['id']]);
          
  
          if(empty($list)){
              return json(['status' => 0, 'msg' =>"客户不存在"]);
          }

          if(!empty($list['content'])){
            $list['content'] = str_replace("api_client",$domain."/api_client",$list['content']);
          }
  
          
          return json(['status'=> 1,'data'=>$list]);
  
  
      }

      //修改客户
    public function updateClient()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();
       

        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传客户ID"]);
        }
  
        $list = $this->Client->getOne(['id' =>$data['id']]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"客户不存在"]);
        }

        if(empty($data['name'])){
            return json(['status' => 0, 'msg' =>"请上传客户名称"]);
        }


        if(empty($data['type'])){
            return json(['status' => 0, 'msg' =>"选择客户类型"]);
        }

        $save = [
            "name"=>$data['name'],
            "created_at"=>date("Y-m-d H:i:s"),
            "update_at"=>date("Y-m-d H:i:s"),
        ];

        if($data['type'] == 1){
            if(empty($data['riqi'])){
                return json(['status' => 0, 'msg' =>"请上传日期"]);
            }

            if(empty($data['content'])){
                return json(['status' => 0, 'msg' =>"请上传订单详情"]);
            }

            if(empty($data['price'])){
                return json(['status' => 0, 'msg' =>"请上传金额"]);
            }

            $save['riqi'] = $data['riqi'];
            $save['content'] = $data['content'];
            $save['price'] = $data['price'];

            if(!empty($data['tel'])){
                $save['tel'] = $data['tel'];
            }else{
                $save['tel'] = null;
            }
        }else{
            if(empty($data['title'])){
                return json(['status' => 0, 'msg' =>"请上传提醒标题"]);
            }

            if(empty($data['commemorative_date'])){
                return json(['status' => 0, 'msg' =>"请上传纪念日期"]);
            }

            if(empty($data['reminder_date'])){
                return json(['status' => 0, 'msg' =>"请上传提醒日期"]);
            }

            $save['title'] = $data['title'];
            $save['commemorative_date'] = $data['commemorative_date'];
            $save['reminder_date'] = $data['reminder_date'];

            if(!empty($data['tel'])){
                $save['tel'] = $data['tel'];
            }else{
                $save['tel'] = null;
            }

            if(!empty($data['address'])){
                $save['address'] = $data['address'];
            }else{
                $save['address'] = null;
            }

            if(!empty($data['remark'])){
                $save['remark'] = $data['remark'];
            }else{
                $save['remark'] = null;
            }

        }

        if(!empty($save['content'])){
            $save['content'] = str_replace($domain."/api_client","api_client",$list['content']);
        }


        $res = $this->Client->where(['id'=>$data['id']])->update($save);
        
        return json(['status' => 1, 'msg' => '操作成功']);

    }


    // 删除客户
    public function  delClient()
    {
        // 判断店铺是否正常
        $is_store = $this->storeIsNormal($this->uid);

        if($is_store['code'] == 1){
            return json(['status' => 0, 'msg' =>$is_store['msg']]);
        }

        $store_id = $is_store['store_id'];

        $data = request()->post();


        if(empty($data['id'])){
            return json(['status' => 0, 'msg' =>"请上传客户ID"]);
        }

  
        $list = $this->Client->getOne(['id' =>$data['id']]);

        if(empty($list)){
            return json(['status' => 0, 'msg' =>"客户不存在"]);
        }


        $res = $this->Client->where(['id'=>$data['id']])->update(['deleted_at'=>date("Y-m-d H:i:s")]);
        return json(['status' => 1, 'msg' => '操作成功']);
    }

    

    // 上传客户图片或者视频
    public function addFile()
    {
        $file = request()->file('file');

        if (!$file) {
            return ['status' => 0, 'msg' => '上传失败，图片不存在'];
        }

        // 获取文件原始扩展名
        $extension = $file->extension();

        $hand = new Handle();

        $dirname = "api_client";

        $savename = $hand->qiniuImg("file", $dirname);

        $add['file_name'] = $savename['fileName'];
        $add['upload_type'] = 2;
        $add['file_path'] = $savename['url'];
        $add['file_type'] = $extension;
        $add['ip'] = Request::ip();
        $add['add_time'] = time();
        $add['user_id'] = $this->uid;
        
        $res = $this->FileLog->create($add);
        $fileLogId = $res->id;
        $savename['file_id'] = $fileLogId;

        return  json($savename);
    }

  

   
    
}
